package com.compty.paint.web.member;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.compty.core.struts2.BaseAction;
import com.compty.paint.domain.Bid;
import com.compty.paint.domain.BidRecord;
import com.compty.paint.domain.Goods;
import com.compty.paint.domain.support.AuditStatus;
import com.compty.paint.manager.BidManager;
import com.compty.paint.manager.BidRecordManager;
import com.compty.paint.manager.GoodsManager;
import com.compty.paint.util.Constants;
import com.compty.security.domain.User;

/**
 * @Todo:拍卖action
 * @CreateTime:2012-2-24 下午02:17:29
 */
public class BidAction  extends BaseAction<Bid> {

	private static final long serialVersionUID = -6718069239545109568L;
	
	private static Log logger = LogFactory.getLog(BidAction.class);
	
	private BidManager bidManager;
	private BidRecordManager bidRecordManager;
	private GoodsManager goodsManager;
	
	
	private Bid bid;
	private Date startDate;
	private Date endDate;
	private String operateStr; // 1表示隐藏,2表示删除
	
	public String index(){
		return "index";
	}
	
	
	/**
	 *@Todo:添加文章  prepare
	 *@CreateTime:2011-9-16 下午03:51:41
	 */
	public String addPrepare(){
		
		this.bid = null;
		
		return "addPrepare";
	}
	
	/**
	 *@Todo:分页列出拍品
	 *@CreateTime:2011-9-16 上午09:33:11
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String list(){
		
		Object user_obj =  session.getAttribute(Constants.Userinfo);
		User userInfo = user_obj !=  null ? (User)user_obj:null;
		
		String bid_status = request.getParameter("bid_status");

        this.page =  bidManager.queryBidForPage(curPage, pageSize,userInfo,startDate,endDate,filterTxt,filterValue,bid_status);
        List list = (List)page.getResult();
        for (int i = 0; i < list.size(); i++) {
        	Bid bid =  (Bid) list.get(i);
        	
        	Integer goods_id = bid.getGoods_id();
        	if( goods_id != null ){
        		Goods goods = goodsManager.findById(goods_id);
        		bid.setGoods(goods);
        		
        		Long current_bidrecord_id = bid.getCurrent_bidrecord_id();
				BidRecord  currentBidRecord = bidRecordManager.get(current_bidrecord_id);
				bid.setCurrentBidRecord(currentBidRecord);
        	}
		}
        
        return "list";
	}
	
	/**
	 *@Todo:保存拍品
	 *@CreateTime:2011-9-16 上午09:44:34
	 * @return
	 */
	public String save(){
		
		if( bid != null){
			
			User userInfo = (User) session.getAttribute(Constants.Userinfo);
			bid.setEntryPerson(userInfo);
			
			bid.setUploadTime(Calendar.getInstance().getTime());
			bid.setModifyTime(Calendar.getInstance().getTime());
			
			bid.setAudit_status(2);
			bid.setBid_status(1);
			bid.setStatus(1);
			
			bidManager.save(bid);
			
			logger.debug("拍品保存成功");
		}
		
		return "saved";
	}
	
	/**
	 *@Todo:预修改拍卖基本信息，显示详情
	 *@CreateTime:2011-9-16 上午09:45:12
	 * @return
	 */
	public String modifyPrepare(){
		
		if( bid != null){
			
			Long id = bid.getId();
			this.bid = bidManager.get(id);
			
        	Integer goods_id = bid.getGoods_id();
        	if(goods_id != null){
        		Goods goods = goodsManager.findById(goods_id);
        		bid.setGoods(goods);
        	}
		}
		
		return "modifyPreare";
	}
	
	/**
	 *@Todo:修改拍品
	 *@CreateTime:2011-9-16 上午09:49:33
	 * @return
	 */
	public String modify(){
		
		if( bid != null){
			
			Long id = bid.getId();
			
			Bid bid_new = bidManager.get(id);
			
			bid_new.setTitle(bid.getTitle());
			bid_new.setBeginPrice(bid.getBeginPrice());
			bid_new.setGoods_id(bid.getGoods_id());
			
			bid_new.setBeginTime(bid.getBeginTime());
			bid_new.setEndTime(bid.getEndTime());
			bid_new.setModifyTime(Calendar.getInstance().getTime());
			
			bidManager.save(bid_new); //保存修改后的文章信息
			
		}
		
		return "modified";
	}
	
	/**
	 *@Todo:修改拍品状态
	 *@CreateTime:2011-6-9 上午10:19:17
	 */
	public String changeUploadState() {

		if ( bid != null) {
			Long id = bid.getId();
			this.bid = bidManager.get(id);

			if ("1".equals(operateStr)) {//隐藏
				bid.setModifyTime(Calendar.getInstance().getTime());
				bidManager.save(bid);
			} else if ("2".equals(operateStr)) { //移除
				bid.setStatus(2);
				bidManager.save(bid);
			}
			else if("3".equals(operateStr)){//通过
				bid.setModifyTime(Calendar.getInstance().getTime());
				bid.setAudit_status(AuditStatus.AUDIT_PASS);
				bidManager.save(bid);
			}
			else if("4".equals(operateStr)){//驳回
				bid.setModifyTime(Calendar.getInstance().getTime());
				bid.setAudit_status(AuditStatus.AUDIT_OVERRULE);
				bidManager.save(bid);
			}
			else if("5".equals(operateStr)){//显示
				bid.setModifyTime(Calendar.getInstance().getTime());
				bidManager.save(bid);
			}
		}
		
		return "changeState";
	}
	
	public void setBidManager(BidManager bidManager) {
		this.bidManager = bidManager;
	}
	
	public void setBidRecordManager(BidRecordManager bidRecordManager) {
		this.bidRecordManager = bidRecordManager;
	}

	public void setGoodsManager(GoodsManager goodsManager) {
		this.goodsManager = goodsManager;
	}

	public Bid getBid() {
		return bid;
	}

	public void setBid(Bid bid) {
		this.bid = bid;
	}

	public String getOperateStr() {
		return operateStr;
	}

	public void setOperateStr(String operateStr) {
		this.operateStr = operateStr;
	}
	
	public Date getStartDate() {
		return startDate;
	}
	
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	
	public Date getEndDate() {
		return endDate;
	}
	
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	
}
